package net.jonbuck.tassoo.ui.view.page;

import net.jonbuck.tassoo.eventmanager.EventManager;
import net.jonbuck.tassoo.eventmanager.EventManagerImpl;
import net.jonbuck.tassoo.eventmanager.events.CategoriesUpdatedEvent;
import net.jonbuck.tassoo.eventmanager.events.PrioritiesUpdatedEvent;
import net.jonbuck.tassoo.eventmanager.events.StatusesUpdatedEvent;
import net.jonbuck.tassoo.eventmanager.events.TypesUpdatedEvent;
import net.jonbuck.tassoo.eventmanager.listener.CategoriesUpdatedEventListener;
import net.jonbuck.tassoo.eventmanager.listener.PrioritiesUpdatedEventListener;
import net.jonbuck.tassoo.eventmanager.listener.StatusesUpdatedEventListener;
import net.jonbuck.tassoo.eventmanager.listener.TypesUpdatedEventListener;
import net.jonbuck.tassoo.model.Category;
import net.jonbuck.tassoo.model.Priority;
import net.jonbuck.tassoo.model.Status;
import net.jonbuck.tassoo.model.Task;
import net.jonbuck.tassoo.model.TassooFactory;
import net.jonbuck.tassoo.model.Type;
import net.jonbuck.tassoo.persistence.dao.TassooDao;
import net.jonbuck.tassoo.persistence.dao.TassooDaoImpl;
import net.jonbuck.tassoo.persistence.dao.util.TassooDaoHelperUtil;
import net.jonbuck.tassoo.persistence.exception.TassooQueryException;
import net.jonbuck.tassoo.ui.util.CalendarComboWidgetValueProperty;
import net.jonbuck.tassoo.ui.util.CommonFonts;
import net.jonbuck.tassoo.ui.util.CommonImages;
import net.jonbuck.tassoo.ui.util.DateFieldHelper;
import net.jonbuck.tassoo.ui.util.TimeTextWidgetValueProperty;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.nebula.timetext.TimeText;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * 
 * @since 1.0.0
 */
public class TaskGeneralTab extends Composite implements CategoriesUpdatedEventListener, PrioritiesUpdatedEventListener,
		StatusesUpdatedEventListener, TypesUpdatedEventListener {

	/** ...TODO... */
	private static final String CATEGORIES_PREFERENCE_PAGE_ID = "net.jonbuck.tassoo.ui.page.category";

	/** ...TODO... */
	protected static final String[] PERCENTAGES = { "0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%" };

	/** ...TODO... */
	private static final String PRIORITIES_PREFERENCE_PAGE_ID = "net.jonbuck.tassoo.ui.page.priorities";

	/** ...TODO... */
	private static final String STATUS_PREFERENCE_PAGE_ID = "net.jonbuck.tassoo.ui.page.status";

	/** ...TODO... */
	private static final String TYPES_PREFERENCE_PAGE_ID = "net.jonbuck.tassoo.ui.page.types";

	private Control actualTimeText;

	/** ... TODO Javadoc ... */
	private Combo categoryCombo;

	/**
	 * <p>
	 * <b>Update value strategy for the Category field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy categoryModelToTargetStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					Category category = (Category) fromObject;
					return category.getValue();
				}

				public Object getFromType() {
					return TassooFactory.eINSTANCE.createCategory();
				}

				public Object getToType() {
					return new String();
				}
			};
			return this;
		}
	};

	/**
	 * <p>
	 * <b>Update value strategy for the Category field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy categoryTargetToModelStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					String categoryValue = (String) fromObject;
					Category category = null;
					try {
						category = TassooDaoHelperUtil.getCategoryByValue(tassooDao, categoryValue);
					} catch (TassooQueryException tqe) {
						System.out.println(tqe.getMessage());
					}
					return category;
				}

				public Object getFromType() {
					return new String();
				}

				public Object getToType() {
					return Category.class;
				}
			};
			return this;
		}
	};

	/** ...TODO Javadoc... */
	private CalendarCombo dueDate;

	/** ...TODO Javadoc... */
	private Control estimatedTimeText;

	/** ...TODO Javadoc... */
	private EventManager eventManager = EventManagerImpl.getInstance();

	/** ...TODO Javadoc ... */
	private DataBindingContext m_bindingContext;

	/** ... TODO Javadoc ... */
	private int position;

	/** ...TODO Javadoc ... */
	private Combo priorityCombo;

	/**
	 * <p>
	 * <b>Update value strategy for the Priority field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy priorityModelToTargetStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					Priority priority = (Priority) fromObject;
					return priority.getValue();
				}

				public Object getFromType() {
					return TassooFactory.eINSTANCE.createPriority();
				}

				public Object getToType() {
					return new String();
				}
			};
			return this;
		}
	};

	/**
	 * <p>
	 * <b>Update value strategy for the Priority field when converting the value
	 * from the UI(Target) to the Model.</b>
	 * </p>
	 */
	private UpdateValueStrategy priorityTargetToModelStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					String priorityValue = (String) fromObject;
					Priority priority = null;
					try {
						priority = TassooDaoHelperUtil.getPriorityByValue(tassooDao, priorityValue);
					} catch (TassooQueryException tqe) {
						System.out.println(tqe.getMessage());
					}
					return priority;
				}

				public Object getFromType() {
					return new String();
				}

				public Object getToType() {
					return TassooFactory.eINSTANCE.createPriority();
				}
			};
			return this;
		}
	};

	/** ...TODO Javadoc... */
	private CalendarCombo startDate;

	/** ...TODO Javadoc... */
	private Combo statusCombo;

	/**
	 * <p>
	 * <b>Update value strategy for the Status field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy statusModelToTargetStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					Status status = (Status) fromObject;
					return status.getValue();
				}

				public Object getFromType() {
					return TassooFactory.eINSTANCE.createStatus();
				}

				public Object getToType() {
					return new String();
				}
			};
			return this;
		}
	};

	/**
	 * <p>
	 * <b>Update value strategy for the Status field when converting the value
	 * from the UI(Target) to the Model.</b>
	 * </p>
	 */
	private UpdateValueStrategy statusTargetToModelStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					String statusValue = (String) fromObject;
					Status status = null;
					try {
						status = TassooDaoHelperUtil.getStatusByValue(tassooDao, statusValue);
					} catch (TassooQueryException tqe) {
						System.out.println(tqe.getMessage());
					}
					return status;
				}

				public Object getFromType() {
					return String.class;
				}

				public Object getToType() {
					return TassooFactory.eINSTANCE.createStatus();
				}
			};
			return this;
		}
	};

	/** ... TODO Javadoc ... */
	private Text summaryText;

	/** ...TODO Javadoc... */
	private Task task;

	/** ... TODO Javadoc ... */
	private Text taskNameText;

	/** ... TODO Javadoc ... */
	private TassooDao tassooDao = TassooDaoImpl.getInstance();

	/** ... TODO Javadoc ... */
	private Combo typeCombo;

	/**
	 * <p>
	 * <b>Update value strategy for the Type field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy typeModelToTargetStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					Type type = (Type) fromObject;
					return type.getValue();
				}

				public Object getFromType() {
					return TassooFactory.eINSTANCE.createType();
				}

				public Object getToType() {
					return new String();
				}
			};
			return this;
		}
	};

	/**
	 * <p>
	 * <b>Update value strategy for the Category field when converting the value
	 * from the Model to the UI(Target).</b>
	 * </p>
	 */
	private UpdateValueStrategy typeTargetToModelStrategy = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object convert(Object fromObject) {
					String typeValue = (String) fromObject;
					Type type = null;
					try {
						type = TassooDaoHelperUtil.getTypeByValue(tassooDao, typeValue);
					} catch (TassooQueryException tqe) {
						System.out.println(tqe.getMessage());
					}
					return type;
				}

				public Object getFromType() {
					return new String();
				}

				public Object getToType() {
					return TassooFactory.eINSTANCE.createType();
				}
			};
			return this;
		}
	};

	/** ... TODO Javadoc ... */
	private Form generalForm;

	/**
	 * 
	 * @param parent
	 * @param form
	 * @param i
	 * @param style
	 */
	public TaskGeneralTab(Composite parent, Form form, int position) {
		super(parent, SWT.NONE);
		this.generalForm = form;
		this.position = position;
		init();
	}

	/**
	 * 
	 * @param parent
	 */
	private void createControl(Composite parent) {
		Composite basicGroup = new Composite(parent, SWT.NONE);
		basicGroup.setLayout(new GridLayout());
		basicGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));

		Label label = new Label(basicGroup, SWT.NONE);
		label.setText("Task Name:");
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
		label.setFont(CommonFonts.LABEL);

		taskNameText = new Text(basicGroup, SWT.BORDER);
		GridData gd_taskNameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_taskNameText.heightHint = 15;
		taskNameText.setLayoutData(gd_taskNameText);

		Label summaryLabel = new Label(basicGroup, SWT.NONE);
		summaryLabel.setText("Summary:");
		summaryLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BEGINNING, false, false));
		summaryLabel.setFont(CommonFonts.LABEL);

		summaryText = new Text(basicGroup, SWT.BORDER | SWT.MULTI | SWT.WRAP);
		GridData summaryTextData = new GridData(SWT.FILL, SWT.FILL, false, false);
		summaryTextData.heightHint = 40;
		summaryTextData.widthHint = 1;
		summaryText.setLayoutData(summaryTextData);

		// Column 1
		Composite fieldsComposite = new Composite(parent, SWT.NONE);
		GridLayout gl_colOneComposite = new GridLayout(8, false);
		gl_colOneComposite.marginTop = 0;
		gl_colOneComposite.marginHeight = 0;
		fieldsComposite.setLayout(gl_colOneComposite);
		fieldsComposite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		Label categoryLabel = new Label(fieldsComposite, SWT.NONE);
		categoryLabel.setText("Category:");
		categoryLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		categoryLabel.setFont(CommonFonts.LABEL);

		Label typeLabel = new Label(fieldsComposite, SWT.NONE);
		typeLabel.setText("Type:");
		typeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		typeLabel.setFont(CommonFonts.LABEL);

		Label lblStartDate = new Label(fieldsComposite, SWT.NONE);
		lblStartDate.setText("Start Date:");
		lblStartDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		lblStartDate.setFont(CommonFonts.LABEL);

		Label dueDateLabel = new Label(fieldsComposite, SWT.NONE);
		dueDateLabel.setText("Due Date:");
		dueDateLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		dueDateLabel.setFont(CommonFonts.LABEL);

		categoryCombo = new Combo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_categoryCombo = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_categoryCombo.minimumWidth = 180;
		categoryCombo.setLayoutData(gd_categoryCombo);

		Button manageCategoriesButton = new Button(fieldsComposite, SWT.PUSH);
		manageCategoriesButton.setImage(CommonImages.getImage(CommonImages.FOLDER));
		manageCategoriesButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				openPreferencePage(CATEGORIES_PREFERENCE_PAGE_ID);
			}
		});

		typeCombo = new Combo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_typeCombo = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_typeCombo.minimumWidth = 180;
		typeCombo.setLayoutData(gd_typeCombo);

		Button manageTypesButton = new Button(fieldsComposite, SWT.PUSH);
		manageTypesButton.setImage(CommonImages.getImage(CommonImages.TAG_PURPLE));
		manageTypesButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				openPreferencePage(TYPES_PREFERENCE_PAGE_ID);
			}
		});

		startDate = new CalendarCombo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		startDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		final Button startDateHelper = new Button(fieldsComposite, SWT.PUSH);
		startDateHelper.setImage(CommonImages.getImage(CommonImages.CALENDAR));
		startDateHelper.addSelectionListener(new DateFieldHelper(startDate, startDateHelper));

		dueDate = new CalendarCombo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		dueDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		final Button dueDateHelper = new Button(fieldsComposite, SWT.PUSH);
		dueDateHelper.setImage(CommonImages.getImage(CommonImages.CALENDAR));
		dueDateHelper.addSelectionListener(new DateFieldHelper(dueDate, dueDateHelper));

		// Second Row
		Label priorityLabel = new Label(fieldsComposite, SWT.NONE);
		priorityLabel.setText("Priority:");
		priorityLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		priorityLabel.setFont(CommonFonts.LABEL);

		Label statusLabel = new Label(fieldsComposite, SWT.NONE);
		statusLabel.setText("Status:");
		statusLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		statusLabel.setFont(CommonFonts.LABEL);

		Label estimatedTime = new Label(fieldsComposite, SWT.NONE);
		estimatedTime.setText("Estimated Time:");
		estimatedTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		estimatedTime.setFont(CommonFonts.LABEL);

		Label actualTime = new Label(fieldsComposite, SWT.NONE);
		actualTime.setText("Actual Time:");
		actualTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		actualTime.setFont(CommonFonts.LABEL);

		priorityCombo = new Combo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		priorityCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Button managePrioritiesButton = new Button(fieldsComposite, SWT.PUSH);
		managePrioritiesButton.setImage(CommonImages.getImage(CommonImages.ERROR));
		managePrioritiesButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				openPreferencePage(PRIORITIES_PREFERENCE_PAGE_ID);
			}
		});

		statusCombo = new Combo(fieldsComposite, SWT.BORDER | SWT.READ_ONLY);
		statusCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Button manageStatusButton = new Button(fieldsComposite, SWT.PUSH);
		manageStatusButton.setImage(CommonImages.getImage(CommonImages.TRAFFIC_LIGHT));
		manageStatusButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				openPreferencePage(STATUS_PREFERENCE_PAGE_ID);
			}
		});

		estimatedTimeText = new TimeText(fieldsComposite, SWT.BORDER);
		GridData gd_estimatedTimeText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		estimatedTimeText.setLayoutData(gd_estimatedTimeText);

		actualTimeText = new TimeText(fieldsComposite, SWT.BORDER);
		GridData gd_actualTimeText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		actualTimeText.setLayoutData(gd_actualTimeText);
	}

	/**
	 * 
	 */
	private void init() {
		CTabFolder parentFolder = (CTabFolder) this.getParent();
		parentFolder.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				CTabFolder folder = (CTabFolder) e.widget;
				if (folder.getSelectionIndex() == position) {
					FormToolkit toolkit = new FormToolkit(getDisplay());
					generalForm.getToolBarManager().removeAll();
					generalForm.getToolBarManager().update(true);
					toolkit.decorateFormHeading(generalForm);
					toolkit.dispose();
				}
			}
		});

		GridLayout gl = new GridLayout();
		gl.horizontalSpacing = 0;
		gl.verticalSpacing = 0;
		gl.marginWidth = 0;
		gl.marginHeight = 0;
		setLayout(gl);

		createControl(this);

		eventManager.addListener(CategoriesUpdatedEvent.class, this);
		eventManager.addListener(PrioritiesUpdatedEvent.class, this);
		eventManager.addListener(StatusesUpdatedEvent.class, this);
		eventManager.addListener(TypesUpdatedEvent.class, this);
	}

	/**
	 * 
	 * @return
	 */
	private DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();

		IObservableValue taskNameObserveWidget = WidgetProperties.text(SWT.Modify).observe(taskNameText);
		IObservableValue taskNameObserveValue = BeanProperties.value(Task.class, "taskName").observe(task);
		bindingContext.bindValue(taskNameObserveWidget, taskNameObserveValue, null, null);

		IObservableValue taskSummaryObserveWidget = WidgetProperties.text(SWT.Modify).observe(summaryText);
		IObservableValue taskSummaryObserveValue = BeanProperties.value(Task.class, "taskSummary").observe(task);
		bindingContext.bindValue(taskSummaryObserveWidget, taskSummaryObserveValue, null, null);

		IObservableValue categoryObserveWidget = WidgetProperties.selection().observe(categoryCombo);
		IObservableValue categoryObserveValue = BeanProperties.value(Task.class, "category").observe(task);
		bindingContext.bindValue(categoryObserveWidget, categoryObserveValue, categoryTargetToModelStrategy,
				categoryModelToTargetStrategy);

		IObservableValue priorityObserveWidget = WidgetProperties.selection().observe(priorityCombo);
		IObservableValue priorityObserveValue = BeanProperties.value(Task.class, "priority").observe(task);
		bindingContext.bindValue(priorityObserveWidget, priorityObserveValue, priorityTargetToModelStrategy,
				priorityModelToTargetStrategy);

		IObservableValue typeObserveWidget = WidgetProperties.selection().observe(typeCombo);
		IObservableValue typeObserveValue = BeanProperties.value(Task.class, "type").observe(task);
		bindingContext.bindValue(typeObserveWidget, typeObserveValue, typeTargetToModelStrategy, typeModelToTargetStrategy);

		IObservableValue statusObserveWidget = WidgetProperties.selection().observe(statusCombo);
		IObservableValue statusObserveValue = BeanProperties.value(Task.class, "status").observe(task);
		bindingContext.bindValue(statusObserveWidget, statusObserveValue, statusTargetToModelStrategy, statusModelToTargetStrategy);

		IObservableValue startDateObserveWidget = new CalendarComboWidgetValueProperty().observe(startDate);
		IObservableValue startDateObserveValue = BeanProperties.value(Task.class, "startDate").observe(task);
		bindingContext.bindValue(startDateObserveWidget, startDateObserveValue, null, null);

		IObservableValue dueDateObserveWidget = new CalendarComboWidgetValueProperty().observe(dueDate);
		IObservableValue dueDateObserveValue = BeanProperties.value(Task.class, "dueDate").observe(task);
		bindingContext.bindValue(dueDateObserveWidget, dueDateObserveValue, null, null);

		IObservableValue estimatedTimeTextObserveWidget = new TimeTextWidgetValueProperty().observe(estimatedTimeText);
		IObservableValue estimatedTimeTextObserveValue = BeanProperties.value(Task.class, "estimatedTime").observe(task);
		bindingContext.bindValue(estimatedTimeTextObserveWidget, estimatedTimeTextObserveValue, null, null);

		IObservableValue actualTimeTextObserveWidget = new TimeTextWidgetValueProperty().observe(actualTimeText);
		IObservableValue actualTimeTextObserveValue = BeanProperties.value(Task.class, "actualTime").observe(task);
		bindingContext.bindValue(actualTimeTextObserveWidget, actualTimeTextObserveValue, null, null);

		return bindingContext;
	}

	/**
	 * 
	 * @param preferencePageId
	 */
	private void openPreferencePage(String preferencePageId) {
		final IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

		final Shell shell;
		if (activeWorkbenchWindow == null) {
			shell = null;
		} else {
			shell = activeWorkbenchWindow.getShell();
		}

		final PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(shell, preferencePageId, null, null);
		dialog.open();
	}

	/**
	 * 
	 */
	public void processCategoriesUpdatedEvent() {
		m_bindingContext.dispose();
		categoryCombo.setItems(TassooDaoHelperUtil.getCategoriesForCombo(tassooDao));
		m_bindingContext = initDataBindings();
	}

	/**
	 * 
	 */
	public void processPrioritiesUpdatedEvent() {
		m_bindingContext.dispose();
		priorityCombo.setItems(TassooDaoHelperUtil.getPrioritiesForCombo(tassooDao));
		m_bindingContext = initDataBindings();
	}

	/**
	 * 
	 */
	public void processStatusesUpdatedEvent() {
		m_bindingContext.dispose();
		statusCombo.setItems(TassooDaoHelperUtil.getStatusesForCombo(tassooDao));
		m_bindingContext = initDataBindings();
	}

	/**
	 * 
	 */
	public void processTypesUpdatedEvent() {
		m_bindingContext.dispose();
		typeCombo.setItems(TassooDaoHelperUtil.getTypesForCombo(tassooDao));
		m_bindingContext = initDataBindings();
	}

	/**
	 * 
	 */
	public void setInput(Task task) {
		this.task = task;

		// Initialise the bindings for the selected task.
		if (m_bindingContext != null) {
			m_bindingContext.dispose();
		}
		categoryCombo.setItems(TassooDaoHelperUtil.getCategoriesForCombo(tassooDao));
		priorityCombo.setItems(TassooDaoHelperUtil.getPrioritiesForCombo(tassooDao));
		statusCombo.setItems(TassooDaoHelperUtil.getStatusesForCombo(tassooDao));
		typeCombo.setItems(TassooDaoHelperUtil.getTypesForCombo(tassooDao));
		m_bindingContext = initDataBindings();

		// Switch of the status and percentage complete if the task is a parent
		// task.
		if (task != null && task.getTask().size() >= 1) {
			statusCombo.setEnabled(false);
		} else {
			statusCombo.setEnabled(true);
		}
	}
}
